package com.tarena.lbs.controller;

import com.tarena.lbs.base.response.JsonResult;
import com.tarena.lbs.dto.AdminLoginParam;
import com.tarena.lbs.dto.AdminParam;
import com.tarena.lbs.dto.AdminQuery;
import com.tarena.lbs.vo.AdminVO;
import com.tarena.lbs.service.AdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/v1/admin")
@Slf4j
@Api(tags = "后台用户管理")
public class AdminController {

    @Autowired
    private AdminService adminService;

    @PostMapping("/insert")
    @ApiOperation(value = "新增后台用户", notes = "创建一个新的后台用户账号")
    public JsonResult insert(
            @ApiParam(value = "后台用户新增参数", required = true)
            @RequestBody AdminParam adminParam) {
        log.debug("新增后台用户参数: {}", adminParam);
        adminService.insert(adminParam);
        return JsonResult.ok();
    }

    @GetMapping("/selectAdmin")
    @ApiOperation(value = "查询后台用户列表", notes = "根据查询条件获取后台用户列表")
    public JsonResult selectAdmin(
            @ApiParam(value = "后台用户查询参数", required = false)
            AdminQuery adminQuery) {
        log.debug("查询后台用户参数: {}", adminQuery);
        List<AdminVO> list = adminService.selectAdmin(adminQuery);
        return JsonResult.ok(list);
    }

    @PostMapping("/deleteAdmin/{id}")
    @ApiOperation(value = "删除后台用户", notes = "根据后台用户 ID 删除对应的用户账号")
    public JsonResult deleteAdmin(
            @ApiParam(value = "后台用户 ID", required = true, example = "1")
            @PathVariable Integer id) {
        log.debug("删除后台用户请求 ID: {}", id);
        adminService.delete(id);
        return JsonResult.ok();
    }

    @PostMapping("/AdminLogin")
    @ApiOperation(value = "后台用户登录", notes = "后台用户使用账号密码进行登录验证")
    public JsonResult AdminLogin(
            @ApiParam(value = "后台用户登录参数", required = true)
            @RequestBody AdminLoginParam adminLoginParam) {
        log.debug("后台用户登录请求参数: {}", adminLoginParam);
        AdminVO adminVO = adminService.AdminLogin(adminLoginParam);
        return JsonResult.ok(adminVO);
    }
}
